

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPoolExecutor {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public MyThreadPoolExecutor(int nThread) {
        // 需要根据当前指定的参数, 创建出若干个线程.
        // 创建出的线程要能执行任务
        for (int i = 0; i < nThread; i++) {
            Thread t = new Thread(() -> {
                try {
                    // 循环的从上述的队列中取元素
                    while (true) {
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo32 {
    public static void main(String[] args) {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(10);
        try {
            for (int i = 0; i < 1000; i++) {
                int id = i;
                executor.submit(() -> {
                    System.out.println("第" + id + "个任务" + ", 线程的名字: " + Thread.currentThread().getName());
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}